/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InternalFrame_Servis.java
 *
 * Created on Nov 23, 2011, 8:20:55 PM
 */
package Boundaries;

import Controller.Servis_Controller;
import Entities.Kendaraan;
import Entities.Servis;
import TableModel.KendaraanSiapSewa_List_TableModel;
import TableModel.Kendaraan_List;
import TableModel.Kendaraan_TableModel;
import TableModel.Servis_List;
import TableModel.Servis_TableModel;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Tea-Nose
 */
public class InternalFrame_Servis extends javax.swing.JInternalFrame {

    /** Creates new form InternalFrame_Servis */
    public InternalFrame_Servis() {
        initComponents();
        internalFrame_tableKendaraan.setVisible(false);
        panel_dataServis.setVisible(false);

        GregorianCalendar dateNow = new GregorianCalendar();
        dateChooser_TanggalServis.setMinSelectableDate(dateNow.getTime());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel_ServisForm = new javax.swing.JPanel();
        label_KodeKendLabel = new javax.swing.JLabel();
        tf_KodeKendaraan = new javax.swing.JTextField();
        label_KodeServisLabel = new javax.swing.JLabel();
        dateChooser_TanggalServis = new com.toedter.calendar.JDateChooser();
        label_TanggalServisLabel = new javax.swing.JLabel();
        comboBox_StatusOli = new javax.swing.JComboBox();
        label_StatusOliLabel = new javax.swing.JLabel();
        tf_KodeServis = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_CatatanServis = new javax.swing.JTextArea();
        button_SelectKendButton = new javax.swing.JButton();
        internalFrame_tableKendaraan = new javax.swing.JInternalFrame();
        scrollPane_tableKend = new javax.swing.JScrollPane();
        table_Kendaraan = new javax.swing.JTable();
        panel_dataServis = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        table_DataServis = new javax.swing.JTable();
        button_OkButton = new javax.swing.JButton();
        button_CancelButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        label_CatatanServisLabel1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();

        setPreferredSize(new java.awt.Dimension(1010, 480));
        getContentPane().setLayout(null);

        panel_ServisForm.setBackground(new java.awt.Color(204, 204, 255));
        panel_ServisForm.setLayout(null);

        label_KodeKendLabel.setText("Kode Kendaraan");
        panel_ServisForm.add(label_KodeKendLabel);
        label_KodeKendLabel.setBounds(10, 30, 110, 14);

        tf_KodeKendaraan.setEditable(false);
        panel_ServisForm.add(tf_KodeKendaraan);
        tf_KodeKendaraan.setBounds(120, 30, 90, 20);

        label_KodeServisLabel.setText("Kode Servis");
        panel_ServisForm.add(label_KodeServisLabel);
        label_KodeServisLabel.setBounds(10, 90, 100, 14);
        panel_ServisForm.add(dateChooser_TanggalServis);
        dateChooser_TanggalServis.setBounds(120, 120, 110, 20);

        label_TanggalServisLabel.setText("Tanggal Servis");
        panel_ServisForm.add(label_TanggalServisLabel);
        label_TanggalServisLabel.setBounds(10, 120, 110, 14);

        comboBox_StatusOli.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "YA", "TIDAK" }));
        panel_ServisForm.add(comboBox_StatusOli);
        comboBox_StatusOli.setBounds(120, 150, 110, 20);

        label_StatusOliLabel.setText("Status Oli");
        panel_ServisForm.add(label_StatusOliLabel);
        label_StatusOliLabel.setBounds(10, 150, 100, 14);

        tf_KodeServis.setEditable(false);
        panel_ServisForm.add(tf_KodeServis);
        tf_KodeServis.setBounds(120, 90, 110, 20);

        ta_CatatanServis.setColumns(20);
        ta_CatatanServis.setRows(5);
        jScrollPane1.setViewportView(ta_CatatanServis);

        panel_ServisForm.add(jScrollPane1);
        jScrollPane1.setBounds(120, 190, 170, 90);

        button_SelectKendButton.setText("jButton1");
        button_SelectKendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_SelectKendButtonActionPerformed(evt);
            }
        });
        panel_ServisForm.add(button_SelectKendButton);
        button_SelectKendButton.setBounds(220, 30, 30, 20);

        internalFrame_tableKendaraan.setTitle("Table Kendaraan");
        internalFrame_tableKendaraan.setVisible(true);

        table_Kendaraan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Nomor Polisi", "Merk Kend.", "Tipe Kend.", "Warna Kend.", "Spesifikasi Kend.", "Status Kend."
            }
        ));
        table_Kendaraan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                table_KendaraanMouseClicked(evt);
            }
        });
        scrollPane_tableKend.setViewportView(table_Kendaraan);

        javax.swing.GroupLayout internalFrame_tableKendaraanLayout = new javax.swing.GroupLayout(internalFrame_tableKendaraan.getContentPane());
        internalFrame_tableKendaraan.getContentPane().setLayout(internalFrame_tableKendaraanLayout);
        internalFrame_tableKendaraanLayout.setHorizontalGroup(
            internalFrame_tableKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane_tableKend, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
        );
        internalFrame_tableKendaraanLayout.setVerticalGroup(
            internalFrame_tableKendaraanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane_tableKend, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
        );

        panel_ServisForm.add(internalFrame_tableKendaraan);
        internalFrame_tableKendaraan.setBounds(300, 10, 630, 310);

        panel_dataServis.setBackground(new java.awt.Color(204, 204, 255));
        panel_dataServis.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Servis Kendaraan"));
        panel_dataServis.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        table_DataServis.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Kode Kendaraan", "Kode Servis", "Tanggal Servis", "Status Oli", "Biaya Servis", "Catatan Servis"
            }
        ));
        jScrollPane2.setViewportView(table_DataServis);

        panel_dataServis.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 20, 610, 160));

        panel_ServisForm.add(panel_dataServis);
        panel_dataServis.setBounds(300, 40, 630, 240);

        button_OkButton.setText("Ok");
        button_OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_OkButtonActionPerformed(evt);
            }
        });
        panel_ServisForm.add(button_OkButton);
        button_OkButton.setBounds(10, 320, 80, 23);

        button_CancelButton.setText("Cancel");
        button_CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_CancelButtonActionPerformed(evt);
            }
        });
        panel_ServisForm.add(button_CancelButton);
        button_CancelButton.setBounds(130, 320, 80, 23);
        panel_ServisForm.add(jSeparator1);
        jSeparator1.setBounds(10, 60, 260, 10);

        label_CatatanServisLabel1.setText("Catatan Servis");
        panel_ServisForm.add(label_CatatanServisLabel1);
        label_CatatanServisLabel1.setBounds(10, 220, 120, 14);

        getContentPane().add(panel_ServisForm);
        panel_ServisForm.setBounds(0, 100, 1000, 360);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Boundaries/Servis Kendaraan.jpg"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 950, 103);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(950, 0, 100, 100);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button_OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_OkButtonActionPerformed
        try {
            // TODO add your handling code here:
            Servis servis = new Servis();
            String vNoPolisi = tf_KodeKendaraan.getText();
            if (servis.ambilTanggalServis(vNoPolisi) == null) {

            viewTableServisByNoPolisi(vNoPolisi);
            SimpleDateFormat simpleDate = new SimpleDateFormat("dd-MM-yyyy");
            String tanggal_servis = simpleDate.format(dateChooser_TanggalServis.getDate());
            GregorianCalendar tglNow = new GregorianCalendar();
            String tgl_Now = simpleDate.format(tglNow.getTime());
            int tglSekarang2 = Integer.parseInt(tgl_Now.substring(0, 2));
            int bulanSekarang = Integer.parseInt(tgl_Now.substring(3, 5));
            int tahunSekarang = Integer.parseInt(tgl_Now.substring(6, 10));
            int tglServis = Integer.parseInt(tanggal_servis.substring(0, 2));
            int bulanServis = Integer.parseInt(tanggal_servis.substring(3, 5));
            int tahunServis = Integer.parseInt(tanggal_servis.substring(6, 10));
            if ((bulanServis <= bulanSekarang && tahunServis <= tahunSekarang && tglServis < tglSekarang2) || (tahunServis < tahunSekarang) || (bulanServis < bulanSekarang && tahunServis <= tahunSekarang)) {
                JOptionPane.showMessageDialog(null, "Maaf tanggal servis tidak dapat kurang dari tanggal sekarang");
            } else {
                Servis_Controller servis_Controller = new Servis_Controller();
                servis_Controller.cek_Servis(this);
                button_CancelButton.doClick();
            }
            // SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MMM-yy");
            //JOptionPane.showMessageDialog(null, );
            // SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MMM-yy");
            //JOptionPane.showMessageDialog(null, );
            }else{
                JOptionPane.showMessageDialog(null, "Jadwal Servis Selanjutnya Sudah Ditentukan");
            }

        } catch (SQLException ex) {
            Logger.getLogger(InternalFrame_Servis.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_button_OkButtonActionPerformed

    private void viewTableServisByNoPolisi(String vNo_Polisi) {
        try {
            List<Servis> servises = Servis_List.getServis_List().getServisbyKendaraan(vNo_Polisi);
            Servis_TableModel servis_TableModel = new Servis_TableModel(servises);
            table_DataServis.setModel(servis_TableModel);
        } catch (SQLException ex) {
            Logger.getLogger(InternalFrame_Servis.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void button_SelectKendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_SelectKendButtonActionPerformed
        try {
            // TODO add your handling code here:
            List<Kendaraan> kendaraans = Kendaraan_List.getKendaraanList().getAllKendaraanSiapDisewa();
            KendaraanSiapSewa_List_TableModel kendaraan_TableModel = new KendaraanSiapSewa_List_TableModel(kendaraans);
            table_Kendaraan.setModel(kendaraan_TableModel);
            internalFrame_tableKendaraan.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(InternalFrame_Servis.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_button_SelectKendButtonActionPerformed

    private void table_KendaraanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_KendaraanMouseClicked
        // TODO add your handling code here:
        internalFrame_tableKendaraan.setVisible(false);
        int row = table_Kendaraan.getSelectedRow();
        String vNo_Polisi = table_Kendaraan.getValueAt(row, 0).toString();
        tf_KodeKendaraan.setText(vNo_Polisi);
        viewTableServisByNoPolisi(vNo_Polisi);
        panel_dataServis.setVisible(true);

        Servis servis = new Servis();
        tf_KodeServis.setText(servis.cariMaxKodeServis());
    }//GEN-LAST:event_table_KendaraanMouseClicked

    private void button_CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_CancelButtonActionPerformed
        // TODO add your handling code here:
        tf_KodeKendaraan.setText("");
        tf_KodeServis.setText("");
        ta_CatatanServis.setText("");
        internalFrame_tableKendaraan.setVisible(false);
    }//GEN-LAST:event_button_CancelButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_CancelButton;
    private javax.swing.JButton button_OkButton;
    private javax.swing.JButton button_SelectKendButton;
    public javax.swing.JComboBox comboBox_StatusOli;
    public com.toedter.calendar.JDateChooser dateChooser_TanggalServis;
    private javax.swing.JInternalFrame internalFrame_tableKendaraan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel label_CatatanServisLabel1;
    private javax.swing.JLabel label_KodeKendLabel;
    private javax.swing.JLabel label_KodeServisLabel;
    private javax.swing.JLabel label_StatusOliLabel;
    private javax.swing.JLabel label_TanggalServisLabel;
    private javax.swing.JPanel panel_ServisForm;
    public javax.swing.JPanel panel_dataServis;
    private javax.swing.JScrollPane scrollPane_tableKend;
    public javax.swing.JTextArea ta_CatatanServis;
    public javax.swing.JTable table_DataServis;
    private javax.swing.JTable table_Kendaraan;
    public javax.swing.JTextField tf_KodeKendaraan;
    public javax.swing.JTextField tf_KodeServis;
    // End of variables declaration//GEN-END:variables
}
